package com.bsi.core;


import com.bsi.core.authorization.RequestUtils;
import com.bsi.core.authorization.TokenHandlerArgumentMethodResovler;
import com.bsi.core.converter.DateConverterEx;
import com.bsi.core.exception.ResponseEntityExceptionHandlerEx;
import com.bsi.core.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.Assert;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/*
 * Author: liusongs
 * Date: 2019/12/16 12:05
 * Content: 扩展WebMvcConfigurer
 */

public class WebMvcConfiguerEx implements WebMvcConfigurer {

    @Autowired
    private DateConverterEx dateConverterEx;

    @Autowired
    private TokenHandlerArgumentMethodResovler tokenHandlerArgumentMethodResovler;

    /**
     * 注册token参数解析器
     *
     * @return
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(tokenHandlerArgumentMethodResovler);
    }

    /**
     * 注册日期抓换器
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(dateConverterEx);
    }

    /**
     * 注入全局异常处理器
     *
     * @param resolvers
     */
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.add(new ResponseEntityExceptionHandlerEx());
    }

    @Bean
    @ConditionalOnClass(TokenHandlerArgumentMethodResovler.class)
    @Autowired(required = true)
    TokenHandlerArgumentMethodResovler tokenHandlerArgumentMethodResovler(@Value("${userId}") String userId) {
        Assert.hasText(userId, "userId不能为空,请在common-core.yml配置文件中配置");
        TokenHandlerArgumentMethodResovler tokenHandlerArgumentMethodResovler = new TokenHandlerArgumentMethodResovler();
        tokenHandlerArgumentMethodResovler.setTokenUserId(userId);
        return tokenHandlerArgumentMethodResovler;
    }

    @Bean
    @ConditionalOnClass(DateConverterEx.class)
    DateConverterEx dateConverterEx() {
        return new DateConverterEx();
    }

    @Bean
    @ConditionalOnClass(RequestUtils.class)
    @Autowired(required = true)
    RequestUtils requestUtils(@Value("${userId}") String userId) {
        Assert.hasText(userId, "userId不能为空,请在common-core.yml配置文件中配置");
        RequestUtils redisTemelateUtils = new RequestUtils();
        redisTemelateUtils.setUserId(userId);
        return redisTemelateUtils;
    }

    @Bean
    public SpringContextUtils getSpringContextUtils() {
        return new SpringContextUtils();
    }

  /*  @Bean
    @Primary
    @Autowired
    public ErrorController exceptionController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
        return new ErrorController(errorAttributes,errorProperties);
    }*/
}
